iT邦幫忙

0

[ASPX] 用C#呼叫ReportServer的報表 並下載檔案(可指定位置和檔名)

  • 分享至 

  • xImage
  •  

原使用http://127.0.0.1/ReportServer?/folder_name/report_name&dt=20240730&rs:Format=PDF
會在CLIENT端下載,但無法指定位置和檔名

這次的方法,可以將檔案下載至SERVER端.

使用方法

            string strFileName =@"D:\20240730.PDF";//下載的檔案要放在哪裡,叫什麼名子
            string URL ="http://127.0.0.1/ReportServer?/folder_name/report_name&dt=20240730&rs:Format=PDF";
            string sID ="id";
            string sPwd="pwd";
            outFile(strFileName, URL, sID, sPwd);
private void outFile(string strFileName, string URL, string sID, string sPwd)
        { 
            string strMsg = ""; 
            FileStream stream = null;
            string encoding = string.Empty;
            string extension = string.Empty;
            string mimeType = string.Empty;

            try
            {  
                #region 下載報表 
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(URL);
                webrequest.Credentials = new System.Net.NetworkCredential(sID, sPwd);
                HttpWebResponse WebResponse = (HttpWebResponse)webrequest.GetResponse();

                int MAXSIZE = 0;
                MAXSIZE = (int)WebResponse.ContentLength;

                System.IO.Stream dataStream = WebResponse.GetResponseStream();
                 
                byte[] buffer = new byte[8192]; 
                FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write);
                int size = 0;
                int _TOTALSIZE = 0;
                do
                {

                    size = dataStream.Read(buffer, 0, buffer.Length);
                    _TOTALSIZE += size;
                    double svalue = _TOTALSIZE / MAXSIZE * 100;

                    if (size > 0)
                        fs.Write(buffer, 0, size);

                } while (size > 0);

                fs.Close();

                WebResponse.Close();
                #endregion

            }
            catch (System.Exception ex)
            {
                strMsg = ex.ToString();
                Response.Write(strMsg);

            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }   

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言